package kibahed.uni.mdp.rlalgorithms;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.text.DecimalFormat;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import kibahed.uni.mdp.representation.Action;
import kibahed.uni.mdp.representation.MDPField;
import kibahed.uni.mdp.representation.Position;
import kibahed.uni.mdp.representation.State;

public class Main {

	private static boolean printToTXT = false;

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		if (printToTXT) {
			setOutToTXT();
		}

		Position b1 = new Position(0, 1);
		Position b2 = new Position(0, 3);
		Position b3 = new Position(2, 1);
		Position b4 = new Position(2, 3);
		List<Position> badPositions = new LinkedList<Position>();
		badPositions.add(b4);
		badPositions.add(b3);
		badPositions.add(b2);
		badPositions.add(b1);

		Position n1 = new Position(1, 1);
		Position n2 = new Position(1, 3);
		Position n3 = new Position(2, 2);
		List<Position> noisyPositions = new LinkedList<Position>();
		noisyPositions.add(n1);
		noisyPositions.add(n2);
		noisyPositions.add(n3);

		// noisyPositions.clear();
		// badPositions.clear();

		MDPField mdpField = new MDPField(5, 5, noisyPositions, badPositions);
		mdpField.setGoalStatePosition(new Position(2, 4));

		ARLalgorithm algorithm = new MeanTimeToAbsorbtion(mdpField);
		algorithm.calculate();

		Position start = new Position(0, 0);
		PAlgorithm pAlgorithm = new PAlgorithm(mdpField);
		// pAlgorithm.doWalking(start, 1000);
		pAlgorithm.calculate();
	}

	private static void setOutToTXT() {
		PrintStream out;
		try {
			int n = 1;
			File res = new File("results");
			if (!res.exists()) {
				res.mkdir();
			} else {
				n = res.listFiles().length + 1;
			}

			out = new PrintStream(
					new FileOutputStream("results/evaluation" + n));
			System.setOut(out);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private static void print(MDPField mdpField) {
		DecimalFormat df = new DecimalFormat();
		df.setMaximumFractionDigits(2);
		System.out.println("Action : right");

		for (State s : mdpField.getStateList()) {
			Map<State, Double> probs = s
					.getProbabilitiesWithChosenAction(new Action(2, "right"));
			for (State n : probs.keySet()) {
				System.out.print(" | [" + s.getPosition().getRow() + ","
						+ s.getPosition().getCollum() + "] -> ["
						+ n.getPosition().getRow() + ","
						+ n.getPosition().getCollum() + "] : p = "
						+ df.format(probs.get(n)));
			}
			System.out.println("|");
		}

		System.out.println("Trying to print rewards ");
		for (State s : mdpField.getStateList()) {
			Map<Action, Double> rewards = s.getRewards();
			System.out.println("Rewards in State [" + s.getPosition().getRow()
					+ "," + s.getPosition().getCollum() + "] : ");
			for (Action a : rewards.keySet()) {
				System.out.print(" " + a.getActionName() + " : "
						+ rewards.get(a));
			}
			System.out.print("\n");
		}
	}

}
